0
1
00:00:00,300 --> 00:00:07,290
This lesson will introduce you to one of the cornerstones of programming, namely functions. Functions
1

2
00:00:07,440 --> 00:00:13,740
go by many names and other programming languages functions have been called subroutines, methods procedures,
2

3
00:00:13,860 --> 00:00:15,540
but don't let the jargon get to you.
3

4
00:00:15,570 --> 00:00:20,270
The central idea is always the same. In Shakespeare's Romeo and Juliet,
4

5
00:00:20,310 --> 00:00:25,050
Juliet famously said "A rose by any other name would smell just as sweet".
5

6
00:00:25,200 --> 00:00:27,210
At the time she was talking about Romeo.
6

7
00:00:27,210 --> 00:00:32,670
But had she been a programmer, I'm sure she would have said the same thing about functions. And on that
7

8
00:00:32,670 --> 00:00:33,120
note,
8

9
00:00:33,120 --> 00:00:36,320
I hope you're ready to start your love affair with functions.
9

10
00:00:36,360 --> 00:00:44,920
Our goal in this lesson is to explain exactly what's going on with a line of Python code like this  - show,
10

11
00:00:45,000 --> 00:00:47,430
followed by the parentheses.
11

12
00:00:47,660 --> 00:00:51,220
We're gonna be focusing on the part after the dot.
12

13
00:00:51,220 --> 00:00:57,700
That way, we can break down the programming concepts of functions into bite sized lessons and build up
13

14
00:00:57,700 --> 00:00:59,220
to the big picture.
14

15
00:00:59,650 --> 00:01:00,340
As such,
15

16
00:01:00,490 --> 00:01:07,180
this lesson really sets up the next couple of lessons on arguments, return values and on objects, so
16

17
00:01:07,270 --> 00:01:12,490
you can think of these videos like a combo - like bread and butter, or burgers and fries or a jab and a cross,
17

18
00:01:12,490 --> 00:01:14,470
or Coca-Cola and Mentos.
18

19
00:01:14,470 --> 00:01:16,050
You get the point.
19

20
00:01:16,250 --> 00:01:18,470
So what's so great about functions?
20

21
00:01:18,500 --> 00:01:25,700
Well, imagine I'm your little housekeeping robot because, I don't know, you didn't want to buy a Roomba or
21

22
00:01:25,700 --> 00:01:33,900
something. And if I was a human you could tell me "Hey, go and buy me some milk".
22

23
00:01:34,030 --> 00:01:37,310
That instruction would make sense to a human.
23

24
00:01:37,390 --> 00:01:41,530
A human would understand what to do in order to complete this task.
24

25
00:01:41,770 --> 00:01:48,670
However, when we're giving instructions to a robot, it doesn't quite work like that. A robot doesn't know
25

26
00:01:48,670 --> 00:01:53,710
what getting milk involves, especially if you've bought the base model with no machine learning capabilities.
26

27
00:01:54,790 --> 00:01:59,770
So when you're dealing with a robot, you have to specify every single step of this task, like taking the
27

28
00:01:59,770 --> 00:02:06,640
money, opening the door, closing the door, walking to the store etc etc etc. You have to specify every
28

29
00:02:06,640 --> 00:02:09,170
single instruction.
29

30
00:02:09,170 --> 00:02:12,720
Now, come the next day and you need to get more milk.
30

31
00:02:12,760 --> 00:02:18,250
Well, you have to specify all these instructions again and that would be incredibly annoying and you'd
31

32
00:02:18,250 --> 00:02:22,080
probably bin your housekeeping robot in short order.
32

33
00:02:22,090 --> 00:02:28,360
Here's where functions come to the rescue. With a function you can bundle all these individual instructions
33

34
00:02:28,660 --> 00:02:35,450
into a single instruction, and we can also give this function a descriptive name like get_milk.
34

35
00:02:35,500 --> 00:02:38,810
Now, every time we want to use get_milk,
35

36
00:02:38,830 --> 00:02:41,380
the housekeeping robot knows exactly what to do.
36

37
00:02:41,440 --> 00:02:46,690
It will execute all the instructions contained inside this function.
37

38
00:02:46,690 --> 00:02:53,160
So there's something to note here - what we just described is actually a two step process.
38

39
00:02:53,160 --> 00:03:00,230
First - you have to tell the robot what get_milk means, and second - you have to tell the robot to execute
39

40
00:03:00,560 --> 00:03:02,470
this get_milk instruction.
40

41
00:03:02,510 --> 00:03:12,590
In other words, step 1 is defining or declaring the function and step 2 is using or calling the function.
41

42
00:03:13,910 --> 00:03:20,630
The syntax looks like this - when defining the function you'll always see this keyword - def.
42

43
00:03:20,700 --> 00:03:29,430
Def followed by the name of the function, followed by parentheses, followed by a colon. After the colon,
43

44
00:03:29,580 --> 00:03:30,900
on the lines below,
44

45
00:03:31,050 --> 00:03:33,810
you have the instructions inside the function.
45

46
00:03:33,810 --> 00:03:41,130
This group of statements is going to be indented and the indentation tells us what statements are inside
46

47
00:03:41,130 --> 00:03:42,340
of the function.
47

48
00:03:42,360 --> 00:03:46,430
Now there's actual words for the anatomy of a function.
48

49
00:03:46,770 --> 00:03:54,240
While you and I have got a head and a body a function has got a header and a body - and the header is
49

50
00:03:54,240 --> 00:04:01,860
this line right. It's the line with the def keyword at the top, and the body are all the statements, all
50

51
00:04:01,860 --> 00:04:05,460
the instructions that are inside the function.
51

52
00:04:05,550 --> 00:04:10,700
Basically, all the lines that are indented. That's the body.
52

53
00:04:10,700 --> 00:04:17,090
Now let's take another look at this line of code where it says plt.show(). The parentheses
53

54
00:04:17,180 --> 00:04:23,180
at the end are telling us that show is a very different beast from math.pi, or 
54

55
00:04:23,210 --> 00:04:25,040
hitchikersGuide.theAnswer.
55

56
00:04:25,040 --> 00:04:30,220
And that's because show is a function, because it had these parentheses at the end.
56

57
00:04:30,230 --> 00:04:34,910
Now there's no parentheses after a math.pi, and there's no parentheses after 
57

58
00:04:34,910 --> 00:04:36,200
hitchikersGuide.theAnswer.
58

59
00:04:36,200 --> 00:04:38,890
These two are not functions.
59

60
00:04:39,200 --> 00:04:45,110
So now that we know a little bit about what a function is supposed to look like, let's create our own
60

61
00:04:45,200 --> 00:04:49,130
first function. Scrolling to the bottom of our Python intro notebook,
61

62
00:04:49,130 --> 00:04:58,250
we can write def get_milk, and then parentheses followed by a colon and when we hit Enter
62

63
00:04:58,820 --> 00:05:04,700
we'll get taken to the next line which is indented and here we can print out some instructions like
63

64
00:05:05,570 --> 00:05:08,720
"Open door", and then we can hit Enter again,
64

65
00:05:08,720 --> 00:05:13,820
get taken to the next line which again is indented and follow up with some more instructions that
65

66
00:05:13,820 --> 00:05:25,130
are going to go inside the function, like print("Walk to the store"), and then print("Buy milk on the ground
66

67
00:05:25,130 --> 00:05:26,420
floor")
67

68
00:05:26,830 --> 00:05:30,250
And we'll finish it up with printing
68

69
00:05:30,260 --> 00:05:30,720
"Return
69

70
00:05:33,710 --> 00:05:36,780
with milk galore".
70

71
00:05:37,040 --> 00:05:41,950
So in our function body, we have for individual print statements.
71

72
00:05:41,960 --> 00:05:47,930
Notice that when you hit Enter on your keyboard Jupyter notebook already indents these lines for you
72

73
00:05:48,410 --> 00:05:56,320
and we have an indentation of one, two, three, four spaces. In Python,
73

74
00:05:56,360 --> 00:06:01,930
this indentation or whitespace actually matters quite a bit. Indentation in Python,
74

75
00:06:01,970 --> 00:06:07,050
is like a full stop at the end of an English sentence. Just like the full stop at the end of the sentence,
75

76
00:06:07,070 --> 00:06:12,810
the whitespace helps Python determine where one instruction ends and another one begins.
76

77
00:06:13,070 --> 00:06:18,590
Now, just like Jupyter notebook has shown us, you also want to have four spaces between the beginning
77

78
00:06:18,590 --> 00:06:22,070
of a line and the first print statement.
78

79
00:06:22,460 --> 00:06:27,860
Usually, the text editor that you're working in, or Jupyter notebook in this case, will help you indent
79

80
00:06:27,860 --> 00:06:29,740
this correctly for you.
80

81
00:06:29,870 --> 00:06:33,500
Let's hit Shift+Enter on the cell and see what happens.
81

82
00:06:33,560 --> 00:06:34,550
Nothing, right?
82

83
00:06:34,550 --> 00:06:35,840
Nothing at all.
83

84
00:06:35,840 --> 00:06:40,940
And that's because we've only defined the function - we actually haven't executed it yet.
84

85
00:06:41,060 --> 00:06:44,390
Remember how we were talking about the two step process?
85

86
00:06:44,390 --> 00:06:45,500
Let's call the get_milk
86

87
00:06:45,500 --> 00:06:46,280
function now.
87

88
00:06:47,210 --> 00:06:54,210
With me write get_milk() and hit Shift+Enter.
88

89
00:06:54,380 --> 00:06:58,020
Now I'm noticing that I made a typo, so let's fix this.
89

90
00:06:58,100 --> 00:07:02,320
Gonna hit Shift+Enter again and execute this function again.
90

91
00:07:02,580 --> 00:07:05,360
Okay, that was really bothering me.
91

92
00:07:05,870 --> 00:07:10,790
Below the cell where we're calling the function, where we wrote get_milk()
92

93
00:07:10,790 --> 00:07:15,790
we see the four statements from the function being printed out.
93

94
00:07:15,820 --> 00:07:20,940
The other thing I quite like about Jupyter notebook is that the syntax highlighting here actually helps
94

95
00:07:20,940 --> 00:07:29,390
us differentiate visually and see at first glance on which part we're defining the function and on which
95

96
00:07:29,390 --> 00:07:34,970
part we're calling the function. The part where we're defining the function the function name is highlighted
96

97
00:07:35,210 --> 00:07:38,580
in blue, and the part where we're calling the function,
97

98
00:07:38,900 --> 00:07:42,190
it's regular black Python code.
98

99
00:07:42,200 --> 00:07:45,530
Now let's take it up a step in complexity in the next lesson.
